package com.huawei.M01.iterator.M08;

/**
 * 121. 买卖股票的最佳时机
 *
 * @author 111
 */
public class MaxProfit01 {
    public int maxProfit(int[] prices) {
        int res = 0;
        int min = prices[0];
        for (int i = 0; i < prices.length; i++) {
            if (i + 1 < prices.length && prices[i] < prices[i + 1]) {
                min = Math.min(min, prices[i]);
            } else if (i + 1 < prices.length && prices[i] > prices[i + 1]) {
                res = Math.max(res, prices[i] - min);
                min = Math.min(prices[i], min);
            }
        }
        return Math.max(res, prices[prices.length - 1] - min);
    }
}
